<cfcomponent displayname="AttributeService" output="false">

	<cffunction name="init" returntype="any" output="false" hint="Constructor">
		<cfreturn this />
	</cffunction>

	<cffunction name="createAttributeObject" returntype="any" output="false" hint="Create Attribute Object">
		<cfargument name="componentName" default="actionpacks.projectshen.model.attribute.Attribute">
		<cfset var attributeObject = createObject("component",arguments.componentName)>
		<cfreturn attributeObject />
	</cffunction>

	<cffunction name="createAttributeTypeObject" returntype="any" output="false" hint="Create Attribute Object">
		<cfargument name="componentName" default="actionpacks.projectshen.model.attribute.AttributeType">
		<cfset var attributeTypeObject = createObject("component",arguments.componentName)>
		<cfreturn attributeTypeObject />
	</cffunction>

	<cffunction name="setPersistenceService" returntype="void" access="public" output="false" hint="Import: Persistence Service">
		<cfargument name="attributePersistenceService" required="true"/>
		<cfset variables.attributePersistenceService = arguments.attributePersistenceService />
	</cffunction>

	<cffunction name="getPersistenceService" returntype="any" access="private" output="false" hint="Returns Internal: Persistence Service">
		<cfreturn variables.attributePersistenceService />
	</cffunction>

	<cffunction name="getAttributeTypesQry" access="public" returntype="any" output="false" roles="" hint="Returns: attribute">
		<cfset var attributeTypeObject = createAttributeTypeObject() />
		<cfset var list = getPersistenceService().query(attributeTypeObject) />
	  <cfreturn list />
	</cffunction>
	
	<cffunction name="getAttributesQry" access="public" returntype="any" output="false" roles="" hint="Returns: attribute">
		<cfargument name="attributeIds" default="" />
		<cfargument name="attributeTypeName" default="" />
		<cfset var attributeObject = createAttributeObject() />
		<cfset var attributeTypesQry = getAttributeTypesQry() />
		<cfset var attributesQry = getPersistenceService().query(attributeObject) />
		<cfquery name="attributesQry" dbtype="query">
		  SELECT * FROM attributeTypesQry,attributesQry
		  WHERE attributeTypesQry.attributeTypeID = attributesQry.attributeTypeID
		  <cfif arguments.attributeIds gt "">AND attributeId IN (#arguments.attributeIds#)</cfif>
		  <cfif arguments.attributeTypeName gt "">AND attributeTypeName LIKE '#attributeTypeName#'</cfif>
		</cfquery>
	  <cfreturn attributesQry />
	</cffunction>

	<cffunction name="getAttributeById" access="public" returntype="any" output="false" roles="" hint="Returns: attribute">
		<cfargument name="attributeId" required="true" />
		<cfset var attributeObject = createAttributeObject()>
		<cfset attributeObject.setAttributeId(arguments.attributeId)>
		<cfset attributeObject = getPersistenceService().persisted(attributeObject)>
	  <cfreturn attributeObject>
	</cffunction>

	<cffunction name="getAttributesByIdsQry" access="public" returntype="any" output="false" roles="" hint="Returns: attribute">
		<cfargument name="attributeIds" required="true" />
		<cfargument name="attributeTypeName" default="" />
		<cfset var attributeObject = createAttributeObject() />
		<cfset var attributeTypesQry = getAttributeTypesQry() />
		<cfset var attributesQry = getPersistenceService().query(attributeObject) />
		<cfquery name="attributesQry" dbtype="query">
		  SELECT * FROM attributeTypesQry,attributesQry
		  WHERE attributeTypesQry.attributeTypeID = attributesQry.attributeTypeID
		  <cfif arguments.attributeIds gt "">AND attributeId IN (#arguments.attributeIds#)<cfelse>AND 1 != 1</cfif>
		  <cfif arguments.attributeTypeName gt "">AND attributeTypeName LIKE '#attributeTypeName#'</cfif>
		</cfquery>
	  <cfreturn attributesQry />
	</cffunction>

	<cffunction name="getAttributeBySafeName" access="public" returntype="any" output="false" roles="" hint="Returns: attribute">
		<cfargument name="attributename" required="true" />
		<cfset var attributeObject = createAttributeObject()>
		<cfset attributeObject.setAttributeName(rereplacenocase(arguments.attributename,'[^a-z0-9_]','','all'))>
		<cfset attributeObject = getPersistenceService().persisted(attributeObject)>
	  <cfreturn attributeObject>
	</cffunction>
	
</cfcomponent>